<template>
  <div>
    <input type="number" v-model="nbval"  min="0">
  </div>
</template>

<script>
export default {
  name: "Child5",
  data() {
    return {
      nbval:this.defVal
    }
  },
  watch: {
    nbval(val) {
      this.$emit("input", this.changeParentValue(val))
    }
  },
  methods: {
    changeParentValue(val) {
      let digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
      let unit = ['', '拾', '佰', '仟', '万', '亿', '兆'];
      let head = val < 0 ? '负' : '';
      val = Math.abs(val);

      let chineseStr = '';
      let level = 0;
      while (val > 0) {
        var digitNum = val % 10;
        if (digitNum !== 0 || (digitNum === 0 && chineseStr !== '')) {
          chineseStr = digit[digitNum] + unit[level] + chineseStr;
        } else if (chineseStr !== '') {
          chineseStr = digit[0] + chineseStr;
        }
        val = Math.floor(val / 10);
        if (level < unit.length - 1) level++;
        else {
          level = 1;
          chineseStr = head + '圆' + chineseStr;
        }
      }

      // 去除连续的零和末尾的零
      chineseStr = chineseStr.replace(/零+/g, '零');
      chineseStr = chineseStr.replace(/零(拾|佰|仟|万|亿|兆)/g, '$1');
      chineseStr = chineseStr.replace(/^零+/, '');
      chineseStr = chineseStr.replace(/零+圆$/, '圆');

      return chineseStr;
    }
  }
}
</script>


<style scoped lang="scss">

</style>
